package com.lyf.gateway.constant;


import com.fasterxml.jackson.annotation.JsonCreator;
import com.lyf.common.api.IErrorCode;

import java.util.HashMap;

/**
 * 错误码定义类
 * TODO ,请按DEMO 中的 统一格式处理。
 * @author ASUS
 */
public enum  ErrCode implements IErrorCode {
    SUCCESS(0, "操作成功"),
    // 默认无法匹配的值
    BUTT(999999, "BUTT"),
    USER_IS_FORBIDDEN(300037, "用户被禁用，请联系管理人员");
    private Integer code;
    private String msg;

    private static HashMap<Integer, ErrCode> map = new HashMap<Integer, ErrCode>();

    static {
        for (ErrCode resultCode : ErrCode.values()) {
            map.put(resultCode.getCode(), resultCode);
        }
    }


    ErrCode(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    @JsonCreator
    public static ErrCode fromValue(Integer code) {
        if (null == code) {
            return BUTT;
        }

        ErrCode codeEnum = map.getOrDefault(code, BUTT);
        return codeEnum;
    }


    private ErrCode(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    @Override
    public int getCode() {
        return code;
    }

    @Override
    public String getMsg() {
        return msg;
    }
}
